package com.bwie.service.impl;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.param.UserParam;
import com.bwie.pojo.TbUser;
import com.bwie.service.TbUserService;
import com.bwie.mapper.TbUserMapper;
import com.bwie.utils.MobileMessageUtil;
import com.bwie.utils.R;

import com.bwie.vo.UserVo;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.concurrent.TimeUnit;

/**
* @author HUAWEI
* @description 针对表【tb_user】的数据库操作Service实现
* @createDate 2025-05-19 09:50:50
*/
@Service
public class TbUserServiceImpl extends ServiceImpl<TbUserMapper, TbUser>
    implements TbUserService{
   @Autowired
   private TbUserMapper tbUserMapper;
   @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Override
    public R sendCode(String userMobile) {
        String code = RandomUtil.randomNumbers(4);
        System.out.println("验证码为："+code);
        String codeKey="userCode-"+userMobile;
        stringRedisTemplate.opsForValue().set(codeKey,code,5, TimeUnit.MINUTES);
        MobileMessageUtil.sendMobileMessage(userMobile,code);
        return R.OK(code);
    }

    @Override
    public R login(UserVo userVo) {
        //校验验证码
        String codeKey="userCode-"+userVo.getUserMobile();
        String redisCode = stringRedisTemplate.opsForValue().get(codeKey);
        if(redisCode==null){
            return R.ERROR(500,"验证码已经过期~~");
        }
        if(!redisCode.equals(userVo.getUserCode())){
            return R.ERROR(500,"验证码错误啦~~");
        }
        //校验手机号格式
        if(!Validator.isMobile(userVo.getUserMobile())){
            return R.ERROR(500,"该手机号格式错误啦~~");
        }
        TbUser tbUser = tbUserMapper.selectOne(new LambdaQueryWrapper<TbUser>()
                .eq(TbUser::getUserMobile, userVo.getUserMobile()));
        TbUser tbUser1 = new TbUser();
        if(tbUser==null){
            tbUser1.setUserName("用户名-"+userVo.getUserMobile());
            tbUser1.setUserPass("密码-"+123456);
            tbUser1.setUserMoney(BigDecimal.valueOf(8.88));
            tbUser1.setUserMobile(userVo.getUserMobile());
            tbUserMapper.insert(tbUser1);
        }else {
            //Jwts生成token
            String token  = Jwts.builder()
                    .signWith(SignatureAlgorithm.HS256,"123456")
                    .claim("userMobile", tbUser.getUserMobile())
                    .claim("userName", tbUser.getUserName())
                    .compact();
            tbUser.setToken(token);
            String tokenKey="token-"+tbUser.getUserId();
            stringRedisTemplate.opsForValue().set(tokenKey,token,30,TimeUnit.MINUTES);
        }

        return R.OK(tbUser);
    }

    @Override
    public R getUserList(UserParam userParam) {
        Page<TbUser> page = new Page<>(userParam.getPageNum(), userParam.getPageSize());
        Page<TbUser> tbUserPage = tbUserMapper.selectPage(page, null);
        return R.OK(tbUserPage);
    }

    @Override
    public R updateMoney(Integer userId) {
        tbUserMapper.updateMoney(userId);
        return R.OK();
    }
}




